﻿#region using
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
#endregion

namespace AwesomeTanks.Sources.GameElements
{
    /// <summary>
    /// It is a sprite which is movable.
    /// </summary>
    public class MovableSprite : Sprite
    {

        #region Fields

        public Vector2 Speed { get; set; }
        public Vector2 OldPosition { get; set; }
        private Vector2 oldSpeed;

        public Vector2 OldSpeed
        {
            get { return oldSpeed; }
            set { Speed = value; }
        }        

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="MovableSprite"/> class.
        /// </summary>
        public MovableSprite() { }

        /// <summary>
        /// Initializes a new instance of the <see cref="MovableSprite"/> class.
        /// </summary>
        /// <param name="position">The position of the sprite.</param>
        /// <param name="speed">The speed of the sprite.</param>
        /// <param name="sourceRectangle">The source rectangle.</param>
        public MovableSprite(Texture2D texture, Rectangle? sourceRectangle, Vector2 position, Vector2 speed)
            : base(texture, sourceRectangle,  position)
        {
            this.Speed = speed;
            this.OldSpeed = speed;
            this.OldPosition = position;
        }

        #endregion

        #region Update

        /// <summary>
        /// Updates the sprite position in function of the specified game time.
        /// </summary>
        /// <param name="gameTime">A snapshot of the current time.</param>
        public virtual void Update(GameTime gameTime)
        {
            this.OldRotation = this.Rotation;
            this.OldPosition = this.Position;
            this.Position += this.Speed * (float)gameTime.ElapsedGameTime.TotalMilliseconds;
            Vector2 spriteSize = new Vector2(SourceRectangle.Value.Width, SourceRectangle.Value.Height);
            this.Position = Vector2.Clamp(Position,  spriteSize / 2, GameStateManagementGame.ScreenSize - spriteSize / 2);
        }

        #endregion
    }
}
